<?php
namespace ClubAdmin\Controller;
use Think\Controller;
class CourseController extends BaseController {


  public function courselist(){
    $coursemodel=M('course');
    $pageSize=6;//每页显示的数据个数
        $records=$coursemodel->count(); //查询数据表中数据的总个数
        $Page = new \Think\Page($records,$pageSize);//实例化分页类，传入总数和每页显示的数量
        $list=$coursemodel->order('course_id desc')->limit($Page->firstRow,$pageSize)->select();//进行分页数据查询
        $page1=$Page->show();//分页显示输出
        $this->assign(array('course'=>$list,'page1'=>$page1));//用数组的方式赋值数据集，赋值分页输出
    $this->display();
  }


        public function courseadd($course_title,$course_class,$course_address,$course_explain,$coach_id,$course_flag){
       
        if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name=$info["file"]["savename"];
        }
        else{
            $name="default.jpg";
        }

        $coursemodel = M("course");
        $coursemodel->course_title=$course_title;
        $coursemodel->course_class=$course_class;
        $coursemodel->course_explain=$course_explain;
        $coursemodel->course_date=date('Y-m-d H:i:s');
        $coursemodel->course_address=$course_address;
        $coursemodel->coach_id=$coach_id;
        $coursemodel->course_flag=$course_flag;
        $coursemodel->course_pic=$name;
        $res = $coursemodel->add();
        if (false !== $res){
            $this->success('添加成功',U('Course/courselist'),3);
        }else{
            $this->error('信息填写有误');
        }
      }

      

        public function getdata(){
        return date('Y-m-d H:i:s');
    }


        public function upload(){
        $upload = new \Think\Upload();
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  = './Public/pic/'; 
        $upload->autoSub = false;
        $info   =   $upload->upload();  
        if(!$info) {
            $this->error($upload->getError());
        }
        else{
            return $info;
        }                    
        }

        public function altercourse($id){
          $coursemodel=M('course');
          $course=$coursemodel->where("course_id=$id")->select();
          $this->course=$course;
          session('course_id',$id);
          $this->display();
    }

      public function update(){
      $id['course_id'] = I('session.course_id');
      $coursemodel = M("course");
      $data['course_title'] = I('post.name');
      $data['course_date'] = I('post.date');
      $data['course_address'] = I('post.address');
      $data['course_explain'] = I('post.explain');
      $data['course_phone'] = I('post.phone');
      // $data['course_price'] = I('post.price');
      $data['coach_id']=I('post.coach_id');
      // $date['club_id']=I('post.club_id');
      if ($_FILES['file']['error'] == 0) {
          $logoinfo = $this->upload();
          $pname = $logoinfo['file']['savename'];
      } else {
          $pname = "default.jpg";
      }
        $data['course_pic'] = $pname;
        $res = $coursemodel->where($id)->save($data);
        if (false !== $res){
            $this->success('修改成功','http://localhost/PHPShiZhanDiLiuZu/yuejian6/ClubAdmin/course/courselist.html');
        }else{
            $this->error('信息填写有误');
        }
    }

    public function delcourse($course_id){
    $courseModel= M('course');
    $course=$courseModel->where("course_id=$course_id")->find();
          if ($course["course_pic"]=="default.jpg") 
        {

        }
            else
        {
                unlink(realpath("./Public/pic/".$course["course_pic"]));
        }

    if (false!==$courseModel->where("course_id=$course_id")->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }
 
}?>